package com.example.system.service.impl;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.Result;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.DeleteIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import com.example.common.enums.CodeEnum;
import com.example.common.exception.ServiceException;
import com.example.system.entity.EsDocument;
import com.example.system.service.EsService;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.StringReader;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * @Author 王鹤蒲
 * @Date 2023 12 25
 */

//@Service
//@Slf4j
public class EsServiceImpl implements EsService {

//    @Autowired
//    private ElasticsearchClient client;
//
//    @Autowired
//    private ElasticsearchAsyncClient asyncClient;
//    @Override
//    public boolean createIndex(String indexName) {
//        CreateIndexResponse response=null;
//        try {
//            response =client.indices().create(c->c.index(indexName));
//        } catch (IOException e) {
//            log.info(e.getMessage());
//           throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response.acknowledged();
//    }
//
//    @Override
//    public GetIndexResponse queryIndex(String indexName) {
//        GetIndexResponse response=null;
//        try {
//             response=client.indices().get(g->g.index(indexName));
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response;
//    }
//
//    @Override
//    public Boolean isIndexExist(String indexName) {
//       BooleanResponse response=null;
//        try {
//            response=client.indices().exists(e->e.index(indexName));
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response.value();
//    }
//
//    @Override
//    public Boolean deleteIndex(String indexName) {
//        DeleteIndexResponse response=null;
//        try {
//            response=client.indices().delete(d->d.index(indexName));
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response.acknowledged();
//    }
//
//    @Override
//    public <T> IndexResponse createByFluentDSL(String indexName, String indexId, T document) {
//
//        try {
//            return client.index(index->index.index(indexName).id(indexId).document(document));
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//    }
//
//    @Override
//    public <T> IndexResponse createByBuilderPattern(String indexName, String indexId, T document) {
//        IndexRequest.Builder<Object> indexRequestBuilder=new IndexRequest.Builder<>();
//        indexRequestBuilder.index(indexName);
//        indexRequestBuilder.id(indexId);
//        indexRequestBuilder.document(document);
//        try {
//            return client.index(indexRequestBuilder.build());
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//    }
//
//    @Override
//    public Boolean createByJson(String indexName, String indexId, String jsonContent) {
//        IndexResponse response=null;
//        try {
//            response=client.index(index->index.index(indexName).id(indexId).withJson(new StringReader(jsonContent)));
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response.forcedRefresh();
//    }
//
//    @Override
//    public <T> void createAsync(String indexName, String indexId, T document, BiConsumer<IndexResponse, Throwable> action) {
//             asyncClient.index(index->index.index(indexName).id(indexId).document(document)).whenComplete(action);
//    }
//
//    @Override
//    public <T> BulkResponse bulkCreate(String indexName, List<T> documents) {
//
//        BulkResponse response= null;
//        try {
//            BulkRequest.Builder builder=new BulkRequest.Builder();
//            documents.forEach(document-> {
//            EsDocument esDocument=(EsDocument) document;
//            builder.operations(operation->operation.index(index->index.index(indexName).id(esDocument.getId().toString()).document(esDocument)));
//            });
//            response = client.bulk(builder.build());
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response;
//    }
//
//    @Override
//    public <T> T getById(String indexName, String documentId, Class<T> tClass) {
//        GetResponse<T> response= null;
//        try {
//            response = client.get(g->g.index(indexName).id(documentId),tClass);
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response.found()?response.source():null;
//    }
//
//    @Override
//    public <T> Result updateById(String indexName, String documentId, Class<T> tClass, Map<String, Object> map) {
//        UpdateResponse<T> response= null;
//        try {
//            response = client.update(index -> index.index(indexName).id(documentId).doc(map),tClass);
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response.result();
//    }
//
//    @Override
//    public ObjectNode getObjectNodeById(String indexName, String documentId) {
//        GetResponse<ObjectNode> response= null;
//        try {
//            response = client.get(g->g.index(indexName).id(documentId),ObjectNode.class);
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response.found()?response.source():null;
//    }
//
//    @Override
//    public Boolean deleteById(String indexName, String documentId) {
//        DeleteResponse response= null;
//        try {
//            response = client.delete(d->d.index(indexName).id(documentId));
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response.forcedRefresh();
//    }
//
//    @Override
//    public BulkResponse bulkDeleteByIds(String indexName, List<String> documentIds) {
//        BulkResponse response= null;
//        try {
//            BulkRequest.Builder builder=new BulkRequest.Builder();
//            documentIds.forEach(documentId->builder.operations(operation->operation.delete(d->d.index(indexName).id(documentId))));
//            response = client.bulk(builder.build());
//        } catch (IOException e) {
//            log.info(e.getMessage());
//            throw new ServiceException(CodeEnum.ES_ERROR.getCode(), CodeEnum.ES_ERROR.getMsg());
//        }
//        return response;
//    }
}
